package com.xyw.code.platform.webapps.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyw.code.platform.common.entity.enums.CommonStatusEnum;
import com.xyw.code.platform.common.entity.po.SysLog;
import com.xyw.code.platform.webapps.dao.SysLogMapper;
import com.xyw.code.platform.webapps.service.ISysLogService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 系统日志 服务实现类
 * </p>
 *
 * @author xyw
 * @since 2020-02-07
 */
@Service
public class SysLogServiceImpl extends ServiceImpl<SysLogMapper, SysLog> implements ISysLogService {

    @Override
    public IPage<SysLog> query(Integer page, Integer pageSize, Integer type, String userName, String description) {
        LambdaQueryWrapper<SysLog> queryWrapper = Wrappers.<SysLog>lambdaQuery();
        if(type!=null){
            queryWrapper.eq(SysLog::getType,type);
        }
        if(StringUtils.isNotBlank(userName)){
            queryWrapper.like(SysLog::getUserName,userName);
        }
        if(StringUtils.isNotBlank(description)){
            queryWrapper.like(SysLog::getDescription,description);
        }
        queryWrapper.eq(SysLog::getDelFlag, CommonStatusEnum.NORMAL.getType());

        IPage<SysLog> sysLogIPage = baseMapper.selectPage(new Page<>(page,pageSize),queryWrapper);
        return sysLogIPage;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteByIds(List<String> ids) {
        List<SysLog> list = ids.stream().map(n->{
            SysLog log = new SysLog();
            log.setId(n);
            log.setDelFlag(CommonStatusEnum.DELETE.getType());
            return log;
        }).collect(Collectors.toList());
        return  updateBatchById(list);
    }
}
